﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MVCManage.Api.Models;
using MVCManage.BLL;
using MVCManage.Model;
namespace MVCManage.Api.Controllers
{
    public class DeptController : ApiController
    {
        // GET: api/Dept
        DepartmentBLL bll = new DepartmentBLL();
        public ResultData<List<DepartmentsDto>> Get()
        {
            var list = from n in bll.Query().ToList()
                       select new DepartmentsDto()
                       {
                           DeptID = n.deptID,
                           DeptName = n.deptName,
                            Remark = n.remark,
                             LastTime = n.lastTime,
                              Type = n.type

                       };
            ResultData<List<DepartmentsDto>> result = new ResultData<List<DepartmentsDto>>();
            result.code = 1;
            result.msg = "ok";
            result.data = list.ToList();
            return result;
        }

        [Route("api/Dept/Page")]

        public ResultPageList<DepartmentsDto> GetDeptPage(string name, int page, int size)
        {
            int count = 0;
            var zhi = bll.GetRolesPagelist(name, page, size, out count);
            var list = from n in zhi
                       select new DepartmentsDto()
                       {
                          DeptID=n.deptID,
                          DeptName=n.deptName,
                          Remark = n.remark,
                          LastTime = n.lastTime,
                          Type = n.type

                       };
            ResultPageList<DepartmentsDto> result = new ResultPageList<DepartmentsDto>();
            result.code = 1;
            result.msg = "ok";
            result.count = count;
            result.data = list.ToList();
            return result;


        }

        public Result PostDept([FromBody] DepartmentsDto value)
        {
         Department zhi = new Department();
            zhi.deptName = value.DeptName;
            zhi.type= value.Type;
            zhi.remark = value.Remark;
            zhi.lastTime = DateTime.Now;
            var pd = bll.Add(zhi);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;

        }
        public Result PutDept([FromBody] DepartmentsDto value)
        {
            Department zhi = new Department();
            zhi.deptID= value.DeptID;
            zhi.deptName = value.DeptName;
            zhi.remark = value.Remark;
            zhi.lastTime = DateTime.Now;
           zhi.type = value.Type;
            var pd = bll.Update(zhi);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }

        public Result Delete(int id)
        {
            var pd = bll.Delete(id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }
        [Route("api/Dept/Deletes")]
        public Result Deletes(int[] ids)
        {
            var pd = bll.Deletes(ids);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }


    }
}
